package com.smit.livevideo.sample.util;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

import com.smit.livevideo.sample.CommonData;
import com.smit.livevideo.sample.MyApplication;
import com.smit.utils.LogUtil;

/**
 * Created by smfang on 2015/12/7.
 * 存储iCast share的工具类
 */
public final class ShareValueUtil
{
    private static final String TAG = ShareValueUtil.class.getSimpleName();
    private static String valueString = " valuse:";

    private ShareValueUtil()
    {

    }

    /**
     * 无需share文件名存储share int值
     * @param key 键值
     * @param value 存储的值
     */
    public static void putShareIntValue(String key, int value)
    {
        putShareIntValue(CommonData.PREF_SHARE_NAME, key, value);
    }

    /**
     * 需要share文件名存储share int值
     * @param prefName share文件名
     * @param key 键值
     * @param value 存储的值
     */
    public static void putShareIntValue(String prefName, String key, int value)
    {
        LogUtil.debug(TAG, "putShareIntValue is in value = " + value);
        try
        {
            SharedPreferences sharedPreferences = MyApplication.getInstance().getSharedPreferences(prefName,
                    Context.MODE_PRIVATE);
            Editor editor = sharedPreferences.edit();
            editor.putInt(key, value);
            editor.commit();
        }
        catch (Exception e)
        {
            LogUtil.error(TAG, "putShareIntValue key = " + key + valueString + value);
        }
    }

    /**
     * 无需share文件名存储share Long值
     * @param key 键值
     * @param value 存储的值
     */
    public static void putShareLongValue(String key, long value)
    {
        putShareLongValue(CommonData.PREF_SHARE_NAME, key, value);
    }

    /**
     * 需要share文件名存储share Long值
     * @param prefName share文件名
     * @param key 键值
     * @param value 存储的值
     */
    public static void putShareLongValue(String prefName, String key, long value)
    {
        LogUtil.debug(TAG, "putShareLongValue is in value = " + value);
        try
        {
            SharedPreferences sharedPreferences = MyApplication.getInstance().getSharedPreferences(prefName,
                    Context.MODE_PRIVATE);
            Editor editor = sharedPreferences.edit();
            editor.putLong(key, value);
            editor.commit();
        }
        catch (Exception e)
        {
            LogUtil.error(TAG, "putShareLongValue key = " + key + valueString + value);
        }
    }

    /**
     * 无需share文件名存储share Boolean值
     * @param key 键值
     * @param value 存储的值
     */
    public static void putShareBooleanValue(String key, boolean value)
    {
        putShareBooleanValue(CommonData.PREF_SHARE_NAME, key, value);
    }

    /**
     * 需要share文件名存储share Boolean值
     * @param prefName share文件名
     * @param key 键值
     * @param value 存储的值
     */
    public static void putShareBooleanValue(String prefName, String key, boolean value)
    {
        LogUtil.debug(TAG, "putShareBooleanValue is in value = " + value);
        try
        {
            SharedPreferences sharedPreferences = MyApplication.getInstance().getSharedPreferences(prefName,
                    Context.MODE_PRIVATE);
            Editor editor = sharedPreferences.edit();
            editor.putBoolean(key, value);
            editor.commit();
        }
        catch (Exception e)
        {
            LogUtil.error(TAG, "putShareBooleanValue key = " + key + valueString + value);
        }
    }

    /**
     * 无需share文件名存储share String值
     * @param key 键值
     * @param value 存储的值
     */
    public static void putShareStringValue(String key, String value)
    {
        putShareStringValue(CommonData.PREF_SHARE_NAME, key, value);
    }

    /**
     * 需要share文件名存储share String值
     * @param prefName share文件名
     * @param key 键值
     * @param value 存储的值
     */
    public static void putShareStringValue(String prefName, String key, String value)
    {
        LogUtil.debug(TAG, "putShareStringValue is in value = " + value);
        try
        {
            SharedPreferences sharedPreferences = MyApplication.getInstance().getSharedPreferences(prefName,
                    Context.MODE_PRIVATE);
            Editor editor = sharedPreferences.edit();
            editor.putString(key, value);
            editor.commit();
        }
        catch (Exception e)
        {
            LogUtil.error(TAG, "putShareStringValue key = " + key + valueString + value);
        }
    }

    /**
     *
     * @param key 键值
     * @param value 存储的值
     * @return int
     */
    public static int getShareIntValue(String key, int value)
    {
        return getShareIntValue(CommonData.PREF_SHARE_NAME, key, value);
    }

    /**
     * 需要share文件名获取share Int值
     * @param prefName share文件名
     * @param key 键值
     * @param value 存储的值
     * @return int
     */
    public static int getShareIntValue(String prefName, String key, int value)
    {
        LogUtil.debug(TAG, "getShareIntValue is in value = " + value);
        int ret = 0;
        try
        {
            SharedPreferences sharedPreferences = MyApplication.getInstance().getSharedPreferences(prefName,
                    Context.MODE_PRIVATE);
            ret = sharedPreferences.getInt(key, value);
        }
        catch (Exception e)
        {
            LogUtil.error(TAG, "getShareIntValue key = " + key + valueString + value);
            ret = 0;
        }
        return ret;
    }

    /**
     * 无需share文件名获取share Long值
     * @param key 键值
     * @param value 存储的值
     * @return long
     */
    public static long getShareLongValue(String key, long value)
    {
        return getShareLongValue(CommonData.PREF_SHARE_NAME, key, value);
    }

    /**
     * 需要share文件名获取share Long值
     * @param prefName share文件名
     * @param key 键值
     * @param value 存储的值
     * @return long
     */
    public static long getShareLongValue(String prefName, String key, long value)
    {
        LogUtil.debug(TAG, "getShareLongValue is in value = " + value);
        long ret = 0L;
        try
        {
            SharedPreferences sharedPreferences = MyApplication.getInstance().getSharedPreferences(prefName,
                    Context.MODE_PRIVATE);
            ret = sharedPreferences.getLong(key, value);
        }
        catch (Exception e)
        {
            LogUtil.error(TAG, "getShareLongValue key = " + key + valueString + value);
            ret = 0L;
        }
        return ret;
    }

    /**
     * 无需share文件名获取share Boolean值
     * @param key 键值
     * @param value 存储的值
     * @return boolean
     */
    public static boolean getShareBooleanValue(String key, boolean value)
    {
        return getShareBooleanValue(CommonData.PREF_SHARE_NAME, key, value);
    }

    /**
     * 需要share文件名获取share Boolean值
     * @param prefName share文件名
     * @param key 键值
     * @param value 存储的值
     * @return boolean
     */
    public static boolean getShareBooleanValue(String prefName, String key, boolean value)
    {
        LogUtil.debug(TAG, "getShareBooleanValue is in value = " + value);
        boolean ret = false;
        try
        {
            SharedPreferences sharedPreferences = MyApplication.getInstance().getSharedPreferences(prefName,
                    Context.MODE_PRIVATE);
            ret = sharedPreferences.getBoolean(key, value);
        }
        catch (Exception e)
        {
            LogUtil.error(TAG, "getShareBooleanValue key = " + key + valueString + value);
            ret = false;
        }
        return ret;
    }

    /**
     * 无需share文件名获取share String值
     * @param key 键值
     * @param value 存储的值
     * @return String
     */
    public static String getShareStringValue(String key, String value)
    {
        return getShareStringValue(CommonData.PREF_SHARE_NAME, key, value);
    }

    /**
     * 需要share文件名获取share String值
     * @param prefName share文件名
     * @param key 键值
     * @param value 存储的值
     * @return String
     */
    public static String getShareStringValue(String prefName, String key, String value)
    {
        LogUtil.debug(TAG, "getShareStringValue is in value = " + value);
        String ret = "";
        try
        {
            SharedPreferences sharedPreferences = MyApplication.getInstance().getSharedPreferences(prefName,
                    Context.MODE_PRIVATE);
            ret = sharedPreferences.getString(key, value);
        }
        catch (Exception e)
        {
            LogUtil.error(TAG, "getShareStringValue key = " + key + valueString + value);
            ret = "";
        }
        return ret;
    }

    /**
     * 无需share文件名删除share中某一条值
     * @param key 键值
     */
    public static void removeShareValue(String key)
    {
        removeShareValue(CommonData.PREF_SHARE_NAME, key);
    }

    /**
     * 需要share文件名删除share中某一条值
     * @param prefName share文件名
     * @param key 键值
     */
    public static void removeShareValue(String prefName, String key)
    {
        LogUtil.debug(TAG, "clearShareValue is in key = " + key);
        try
        {
            SharedPreferences sharedPreferences = MyApplication.getInstance().getSharedPreferences(prefName,
                    Context.MODE_PRIVATE);
            Editor editor = sharedPreferences.edit();
            editor.remove(key);
            editor.commit();
        }
        catch (Exception e)
        {
            LogUtil.error(TAG, "clearShareValue Exception");
        }
    }
}
